const request = require('@/utils/request.js')
const md5 = require("@/utils/md5.js");
const constants = require('@/utils/khyConstants.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
      userNoError: '',
      userPwdError: '',
      verifyCodeError: '',
      bgImg: getApp().globalData.nginxIp + '/img/hongbao.jpg',
      smsMeg:'发送验证码',
      phoneNumbers:null,
      minutes:2*60 * 1000,
      isShowGetMeg:true,
      discountValue: 5,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.getDictData()
    },

    getDictData() {
      let dictString = constants.dict.优惠金额
      request.get('/HomeController/getDictData', {dictCode: dictString}).then(res => {
        if (res.data.code == constants.resultSuccessCode &&
          res.data.result) {
          let dictData = res.data.result[constants.dict.优惠金额] || []
          let discountValue = 5
          dictData.forEach(item => {
            discountValue = item['itemValue'] || ''
          });
          this.setData({
            discountValue: discountValue,
          })
        }
      })
    },

    onInput(e) {
      let name = e.currentTarget.dataset.name
      let value = e.detail.value
      if (value) {
        this.setData({
          [`${name}Error`]: '',
          phoneNumbers:value
        })
      }
    },
    onInputs(e) {
        let name = e.currentTarget.dataset.name
        let value = e.detail.value
        if (value) {
          this.setData({
            [`${name}Error`]: ''
          })
        }
      },
    formSubmit(e) {
        let userNo = e.detail.value.userNo
        let userPwd=e.detail.value.userPwd
        let verifyCode = e.detail.value.verifyCode
        if (userNo == null || userNo == '') {
            this.setData({
              userNoError: "error"
            })
            return
        }
        if (userPwd == null || userPwd == '') {
            this.setData({
              userPwdError: "error"
            })
            return
        }

        if (!verifyCode) {
          this.setData({
            verifyCodeError: "error"
          })
          return
        }

        let khyUsers={}
        khyUsers.userNo=userNo
        khyUsers.userPwd=md5.hexMD5(userPwd)
        khyUsers.verifyCode=verifyCode
        request.post('/HomeController/saveKhyUser',khyUsers).then(res => {
            if(res.data.code == "200"){
              wx.showToast({
                title: '注册成功',
                icon: 'success',
                duration: 2000,
                success: res => {
                  setTimeout(function() {
                    wx.navigateBack()
                  }, 2000)
                }
              })
            } else {
              wx.showToast({
                title: res.data.message || '注册失败',
                icon: 'error',
                duration: 2000
              })
            }
          })
    },
    /**
     * 获取验证码
     */
    getSmsCode(){
        let phoneNumbers=this.data.phoneNumbers
        const regex = /^(((1[35789][0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
        if(phoneNumbers == null){
            wx.showToast({
                title:'手机号不能为空',
                icon: 'error',
                duration: 2000
              })
              return
        }
        if(phoneNumbers.length !== 0&& phoneNumbers.length != 11){
            wx.showToast({
                title:'手机长度有误',
                icon: 'error',
                duration: 2000
              })
              return
        }
        if(!regex.test(phoneNumbers)){
            wx.showToast({
                title:'手机号有误',
                icon: 'error',
                duration: 2000
              })
              return
        }
        let jsonObject={}
        jsonObject.phoneNumbers=phoneNumbers
        request.post('/HomeController/getSmsCode',jsonObject).then(res => {
           if(res.data.code == "200"){
            wx.showToast({
                title:'发送成功',
                icon: 'success',
                duration: 2000
              })
              this.setData({
                isShowGetMeg:false
              })
           }else{
            wx.showToast({
                title:res.data.message,
                icon: 'error',
                duration: 2000
              })
           }
        })
    },
    finish(){
        this.setData({
            isShowGetMeg:true
          })
     },
    close(){
        wx.navigateBack({
            data:1
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
